package net.wesley.android.city;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.os.Handler;
import android.os.Message;

import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class Lanzhou extends City {

	private Handler request1handler=new Handler(){
		public void handleMessage(Message message){
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String body=(String)message.obj;
				int index=body.indexOf("处理机关</span>");
				String retvalue="";
				if (index>0){
					String str=body.substring(index+11).split("</TABLE>")[0];
					String[] rows=str.split("<tr");
					for (int i=1;i<rows.length-1;i++){
						String[] cols=rows[i].split("<td");
						String shijian=cols[5].split("</td>")[0].split(">")[1].trim();
						String didian=cols[6].split("</td>")[0].split(">")[1].trim();
						String code=cols[7].split("</td>")[0].split(">")[1].trim();
						retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
					}
					if (rows.length-2==0){
						callback.onComplete(0, "ret:未找到违章记录，注意：兰州市在车辆信息输入错误的时候返回同样的结果");
						return;
					}
					callback.onComplete(rows.length-2, retvalue);
					return;
				}
				break;
			}
		}
	};

	public void requestParamsFinish(String[] values) {
		try {
			new HttpConnection(ctx,request1handler,"GBK").post("http://gsgajt.gov.cn:9080/GajtInter/InfoSerch/infosearch.do?method=vehvioinfo", 
					String.format("hpzl=02&fzjg=%%B8%%CAA&thphm=%s&hphm=%s&clsbdh=%s",
							chepai.substring(1),URLEncoder.encode(chepai,"GBK"),values[0]));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("甘A");
	}

	@Override
	public String[] paramDesc() {
		String[] s={"车辆识别代号"};
		return s;
	}

}
